package com.heifeng.cloudshelf.mvp.programme.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heifeng.cloudshelf.base.MyApp;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.event.OpenDetailEvent;
import com.heifeng.cloudshelf.mvp.model.DetailImageInfo;
import com.heifeng.cloudshelf.utils.DensityUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by admin on 2019/9/12.
 */

public class ProgrammeRvAdapter extends BaseQuickAdapter<DetailImageInfo, BaseViewHolder> {
    private Context context;

    private int type;
//    private List<ImageViewBean> imageViews = new ArrayList<>();

    public ProgrammeRvAdapter(Context context, int layoutResId, @Nullable List<DetailImageInfo> data, int type) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
    }

//    public void cleanView(){
//        for (int i = 0; i < imageViews.size(); i++) {
//            ImageViewBean imageViewBean = imageViews.get(i);
//            if (null != imageViewBean.getIv_top()) {
//                Glide.get(mContext).clearDiskCache();
//                Glide.get(mContext).clearMemory();
//            }
//        }
//        imageViews.clear();
//    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, DetailImageInfo item) {
        if (type != 2 && type != 4) {
            LinearLayout.LayoutParams layoutParamsTop = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParamsTop.height = MyApp.SCREEN_WIDTH / 3 - DensityUtils.dp2px(context, 15);
            layoutParamsTop.width = DensityUtils.dp2px(mContext, 266);
            layoutParamsTop.setMargins(0, DensityUtils.dp2px(context, 20), DensityUtils.dp2px(context, 28), 0);

            LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParamsBottom.height = MyApp.SCREEN_WIDTH / 3 - DensityUtils.dp2px(context, 15);
            layoutParamsTop.width = DensityUtils.dp2px(mContext, 266);
            layoutParamsBottom.setMargins(0, DensityUtils.dp2px(context, 8), DensityUtils.dp2px(context, 28), 0);

            helper.getView(R.id.l_top).setLayoutParams(layoutParamsTop);
            helper.getView(R.id.l_bottom).setLayoutParams(layoutParamsBottom);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.height = DensityUtils.dp2px(mContext, 180);
            layoutParams.width = DensityUtils.dp2px(mContext, 266);
            helper.getView(R.id.iv_top).setLayoutParams(layoutParams);
            helper.getView(R.id.iv_bottom).setLayoutParams(layoutParams);
        } else {
            LinearLayout.LayoutParams layoutParamsTop = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParamsTop.height = DensityUtils.dp2px(mContext, 192);
            layoutParamsTop.width = DensityUtils.dp2px(mContext, 234);
            layoutParamsTop.setMargins(0, DensityUtils.dp2px(context, 20), DensityUtils.dp2px(context, 28), 0);

            LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParamsBottom.height = DensityUtils.dp2px(mContext, 192);
            layoutParamsTop.width = DensityUtils.dp2px(mContext, 234);
            layoutParamsBottom.setMargins(0, DensityUtils.dp2px(context, 20), DensityUtils.dp2px(context, 28), 0);

            helper.getView(R.id.l_top).setLayoutParams(layoutParamsTop);
            helper.getView(R.id.l_bottom).setLayoutParams(layoutParamsBottom);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.height = DensityUtils.dp2px(mContext, 158);
            layoutParams.width = DensityUtils.dp2px(mContext, 234);
            helper.getView(R.id.iv_top).setLayoutParams(layoutParams);
            helper.getView(R.id.iv_bottom).setLayoutParams(layoutParams);
        }

        RoundedCorners roundedCorners = new RoundedCorners(2);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
//        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//        options.skipMemoryCache(true);
//        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE).dontAnimate();

        if (null != item.getFirstImage() && !TextUtils.isEmpty(item.getFirstImage().getImage())) {
            RequestOptions options = new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(mContext).load(item.getFirstImage().getImage()).apply(options).into((ImageView) helper.getView(R.id.iv_top));

            helper.getView(R.id.l_top).setVisibility(View.VISIBLE);

            helper.setText(R.id.tv_top, item.getFirstImage().getName());
            helper.getView(R.id.tv_top).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.l_top).setVisibility(View.INVISIBLE);
            helper.getView(R.id.tv_top).setVisibility(View.INVISIBLE);
        }

        if (null != item.getSecondImage() && !TextUtils.isEmpty(item.getSecondImage().getImage())) {
            RequestOptions options = new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(mContext).load(item.getSecondImage().getImage()).apply(options).into((ImageView) helper.getView(R.id.iv_bottom));

            helper.getView(R.id.l_bottom).setVisibility(View.VISIBLE);

            helper.setText(R.id.tv_bottom, item.getSecondImage().getName());
            helper.getView(R.id.tv_bottom).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.l_bottom).setVisibility(View.INVISIBLE);
            helper.getView(R.id.l_bottom).setVisibility(View.INVISIBLE);
        }

        helper.getView(R.id.iv_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openDetail(item.getFirstImage());
            }
        });

        helper.getView(R.id.iv_bottom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openDetail(item.getSecondImage());
            }
        });
    }

    public void openDetail(Object obj) {
        EventBus.getDefault().post(new OpenDetailEvent(obj));
    }

    public static class ImageViewBean {
        private ImageView iv_top;
        private ImageView iv_bottom;

        public ImageView getIv_top() {
            return iv_top;
        }

        public void setIv_top(ImageView iv_top) {
            this.iv_top = iv_top;
        }

        public ImageView getIv_bottom() {
            return iv_bottom;
        }

        public void setIv_bottom(ImageView iv_bottom) {
            this.iv_bottom = iv_bottom;
        }
    }
}
